IF EXISTS ( SELECT  *
            FROM    dbo.sysobjects
            WHERE   id = OBJECT_ID(N'[CGO_EventSave]')
                    AND OBJECTPROPERTY(id, N'IsProcedure') = 1 ) 
  DROP PROCEDURE CGO_EventSave
GO

SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO



CREATE PROCEDURE dbo.CGO_EventSave
  @EventID INT ,
  @PortalID INT ,
  @UserID INT ,
  @DateBegin DATETIME,
  @DateEnd DATETIME,
  @Name NVARCHAR(100),
  @Description NTEXT,
  @MaxEnrollment INT,
  @EnrollFee MONEY,  
  @EnrollType NVARCHAR(10),
  @ImageURL NVARCHAR(100),
  @ImageType VARCHAR(1),
  @ImageWidth INT,
  @ImageHeight INT,
  @Status INT,
  @CategoryID INT,
  @ImageDisplay BIT,
  @Signups BIT,
  @AllDayEvent BIT,
  @Approved BIT
AS    
BEGIN  
  --IF @DateBegin IS NULL
  --  SET @DateBegin = CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),101))
  
  --IF @DateEnd IS NULL
  --  SET @DateEnd = DATEADD(DAY,1,@DateBegin)
  DECLARE @RetEventID INT
  SET @RetEventID = 0
    
  IF @ImageURL = N'' 
    SET @ImageURL = NULL
    
  IF @ImageType = N'' 
    SET @ImageType = NULL   
    
  IF @EnrollType IS NULL OR @EnrollType=N''
    SET @EnrollType = 'FREE'
  
  IF @ImageDisplay IS NULL
    SET @ImageDisplay = 0
  
  IF @Signups IS NULL
    SET @Signups = 0
  
  IF @AllDayEvent IS NULL
    SET @AllDayEvent = 0
    
  IF @Approved IS NULL
    SET @Approved = 0 
            
  IF @EventID IS NULL OR @EventID =-1 OR @EventID = 0
  BEGIN
  	INSERT INTO dbo.CGO_Event
  	        ( PortalID ,
  	          DateBegin ,
  	          DateEnd ,
  	          Name ,
  	          Description ,
  	          MaxEnrollment ,
  	          EnrollFee ,
  	          EnrollType ,
  	          ImageURL ,
  	          ImageType ,
  	          ImageWidth ,
  	          ImageHeight ,
  	          Status ,
  	          Category ,
  	          ImageDisplay ,
  	          Signups ,
  	          AllDayEvent ,
  	          Approved ,
  	          CreatedByID ,
  	          CreatedDate ,
  	          LastUpdatedID ,
  	          LastUpdateDate
  	        )
  	VALUES  ( @PortalID , -- PortalID - int
  	          @DateBegin , -- DateBegin - datetime
  	          @DateEnd , -- DateEnd - datetime
  	          @Name , -- Name - nvarchar(100)
  	          @Description , -- Description - ntext
  	          @MaxEnrollment , -- MaxEnrollment - int
  	          @EnrollFee , -- EnrollFee - money
  	          @EnrollType , -- EnrollType - nvarchar(10)
  	          @ImageURL , -- ImageURL - nvarchar(100)
  	          @ImageType , -- ImageType - varchar(1)
  	          @ImageWidth , -- ImageWidth - int
  	          @ImageHeight , -- ImageHeight - int
  	          @Status , -- Status - int
  	          @CategoryID , -- Category - int
  	          @ImageDisplay , -- ImageDisplay - bit
  	          @Signups , -- Signups - bit
  	          @AllDayEvent , -- AllDayEvent - bit
  	          @Approved , -- Approved - bit
  	          @UserID , -- CreatedByID - int
  	          GETDATE() , -- CreatedDate - datetime
  	          -1 , -- LastUpdatedID - int
  	          GETDATE()  -- LastUpdateDate - datetime
  	        )
    SET @RetEventID = @@IDENTITY  	        	        
  END
  ELSE
  BEGIN
   UPDATE dbo.CGO_Event
   SET    DateBegin = @DateBegin,
          DateEnd = @DateEnd,
          Name = @Name ,
          Description = @Description ,
          MaxEnrollment = @MaxEnrollment,
          EnrollFee = @EnrollFee,
          EnrollType = @EnrollType,
          ImageURL = @ImageURL,
          ImageType = @ImageType,
          ImageWidth = @ImageWidth,
          ImageHeight = @ImageHeight,
          Status = @Status,
          Category = @CategoryID,
          ImageDisplay = @ImageDisplay,
          Signups = @Signups,
          AllDayEvent = @AllDayEvent,
          Approved = @Approved,
          LastUpdatedID = @UserID,
          LastUpdateDate = GETDATE()
   WHERE  EventID = @EventID
          AND PortalID = @PortalID  	
          
   SET @RetEventID = @EventID
  END
  
  SELECT @RetEventID
END  
GO
